<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="招标项目编号" prop="sCode">
        <el-input
          v-model="queryParams.sCode"
          placeholder="请输入招标项目编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >邀请或取消</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除或取消</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="invitationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="招标公告ID" align="center" prop="uid" />
      <el-table-column label="招标公告" align="center" prop="uuTitle" />
      <el-table-column label="招标项目ID" align="center" prop="sid" />
      <el-table-column label="招标项目编号" align="center" prop="sCode" />
      <el-table-column label="供应商" align="center" prop="hhName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除或取消</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改邀请供应商招投标对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1080px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="招标项目编号" prop="sCode">
          <el-input v-model="form.sCode" placeholder="请输入招标项目编号" disabled/>
        </el-form-item>
        <el-form-item label="招标项目ID" prop="sid" class="form-input">
          <el-input v-model="form.sid" placeholder="请输入招标项目ID" disabled/>
        </el-form-item>
        <el-form-item label="招标项目" prop="sCode" class="form-input">
          <el-input v-model="form.sName" placeholder="请输入招标项目" disabled/>
        </el-form-item>
        <el-form-item label="招标公告ID" prop="uid" class="form-input">
          <el-input v-model="form.uid" placeholder="请输入招标公告ID" disabled/>
        </el-form-item>
        <el-form-item label="招标公告" prop="uTitle" class="form-input">
          <el-input v-model="form.uTitle" placeholder="请输入招标公告" disabled/>
        </el-form-item>
        <p style="font-size: 14px;font-weight: 700;color: #606266;" v-if="this.sWay === 2 ">选择需要邀请的供应商<span style="color: red;">(只能邀请到“已发布”的公告中，且会给新邀请的供应商发邀请邮件，不会给取消的供应商发邮件)</span></p>
        <el-table v-loading="supplierLoading" :row-key="getRowKey" @row-click="clickRow" ref="multipleTable" @selection-change="handleSupplierSelectionChange" :data="supplierList.slice((queryParams3.pageNum-1)*queryParams3.pageSize,queryParams3.pageNum*queryParams3.pageSize)"  v-if="this.sWay === 2 ">
          <el-table-column type="selection" :reserve-selection="true" width="55" align="center" />
          <el-table-column type="index" label="序号" align="center" width="80"/>
          <el-table-column label="供应商名称" align="center" prop="hName"/>
          <el-table-column label="企业性质" align="center" prop="hQuality">
            <template slot-scope="scope">
              {{ scope.row.hQuality ? scope.row.hQuality : '---' }}
            </template>
          </el-table-column>
          <el-table-column label="经营产品" align="center" prop="hInstitution">
            <template slot-scope="scope">
              <el-tag
                size="small"
                disable-transitions v-if="scope.row.hInstitution">{{ scope.row.hInstitution }}
              </el-tag>
              <span v-else>---</span>
            </template>
          </el-table-column>
          <el-table-column label="统一社会信用代码" align="center" prop="hCreditCode"/>
        </el-table>
        <pagination
          v-if="this.sWay === 2 "
          v-show="supplierTotal>0"
          :total="supplierTotal"
          :page.sync="queryParams3.pageNum"
          :limit.sync="queryParams3.pageSize"
        />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listInvitation, getInvitation, delInvitation, addInvitation, updateInvitation, getInvitationByUidSid} from "@/api/system/tender/bidInvitation";
import {getTender} from "@/api/system/tender/tender";
import {listAllSupplier} from "@/api/system/supplier";
import {findStatus} from "@/api/system/tender/tenderNotice";

export default {
  name: "Invitation",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 邀请供应商招投标表格数据
      invitationList: [],
      sWay: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sid: null,
        sCode: null,
        hid: null,
      },
      // 表单参数
      form: {},
      bidTender: {},
      // 表单校验
      rules: {
      },
      supplierLoading:false,
      supplierList: [],
      // 总条数
      supplierTotal: 0,
      // 查询参数
      queryParams3: {
        pageNum: 1,
        pageSize: 10,
      },
      // 选中数组
      supplierIds: [],
      // 非单个禁用
      supplierSingle: true,
      // 非多个禁用
      supplierMultiple: true,
    };
  },
  mounted() {
    // 注册方法
    this.$store.commit('registerInvitationMethod', {
      key: 'flashList',
      method: this.flashList
    })
  },
  created() {
    this.queryParams.sid =this.$route.query.sid;
    this.queryParams.sCode = this.$route.query.sCode;
    this.queryParams.aid =this.$route.query.xyId;
    //do nothing
    getTender(this.queryParams.sid).then(res=>{
      this.loading = false;
      this.bidTender = res.data;
      this.sWay = res.data.sWay;
      if(this.sWay === 2){
        //邀请招标则查询
        this.getList();
      }
    });
  },
  methods: {
    flashList(){
      this.getList();
    },
    /** 查询邀请供应商招投标列表 */
    getList() {
      this.loading = true;
      listInvitation(this.queryParams).then(response => {
        this.invitationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询供应商列表 */
    getSupplierListForEdit(uid,sid,isEdit) {
      this.supplierLoading = true
      this.supplierList = [];
      this.supplierTotal = 0;
      listAllSupplier().then(response => {
        this.supplierList = response.data;
        this.supplierTotal = this.supplierList.length;
        this.supplierLoading = false;
        if(isEdit){
          getInvitationByUidSid(uid,sid).then(res => {
            if(res && res.code === 200 && res.data){
              this.supplierIds = null;
              this.form.hids = null;
              if(this.supplierList.length > 0){
                this.supplierList.forEach(row => {
                  res.data.forEach(item => {
                    if(row.hid === item.hid){
                      this.$refs.multipleTable.toggleRowSelection(row, true);
                    }
                  });
                })
              }
            }
          });
        }
      });
    },
    /** 单击选中行数据 */
    clickRow(row) {
      this.$refs.multipleTable.toggleRowSelection(row);
    },
    // 多选框选中数据
    handleSupplierSelectionChange(selection) {
      this.supplierIds = selection.map(item => item.hid);
      this.supplierSingle = selection.length!==1;
      this.supplierMultiple = !selection.length;
      this.form.hids = this.supplierIds;
    },
    // 保存选中的数据编号
    getRowKey(row) {
      return row.hid;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sid: null,
        sCode: null,
        hid: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      if(this.sWay === 2){
        //邀请招标则新建
        this.reset();
        findStatus(this.bidTender.sid).then(res=>{
          if(res.data.length > 0){
            let uid = res.data[0].uid;
            this.form.uid=res.data[0].uid;
            this.form.uTitle=res.data[0].uTitle;
            this.getSupplierListForEdit(uid,this.form.sid,true);
          }
        });
        this.form.sid = this.bidTender.sid;
        this.form.sCode = this.bidTender.sCode;
        this.form.sName = this.bidTender.sName;

        this.open = true;
        this.title = "邀请供应商招投标";
      }else{
        this.$modal.msgWarning("项目招标方式为公开招标，无需邀请供应商");
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      if(this.sWay === 2){
        //邀请招标则新建
        this.reset();
        const id = row.id || this.ids
        getInvitation(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改邀请供应商招投标";
        });
      }else{
        this.$modal.msgWarning("项目招标方式为公开招标，无需邀请供应商");
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInvitation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.supplierLoading = true;
            addInvitation(this.form).then(response => {
              this.supplierLoading = false;
              this.$modal.msgSuccess("邀请或修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除邀请供应商招投标编号为"' + ids + '"的数据项？').then(function() {
        return delInvitation(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/invitation/export', {
        ...this.queryParams
      }, `invitation_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
